package com.labrecruit.system.service.impl;

import com.labrecruit.system.domain.StuApply;
import com.labrecruit.system.domain.StuInfo;
import com.labrecruit.system.mapper.ApplyMapper;
import com.labrecruit.system.mapper.UserMapper;
import com.labrecruit.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplyMapper applyMapper;

    /**
     * 新增报名信息
     * (姓名学号必填，校验信息是否一致)
     */
    @Override
    public int insertUser (StuApply apply) {
        StuInfo info = userMapper.selectStuByNum(apply.getStuNum());
        if(info != null) {
            if(Objects.equals(info.getName(), apply.getName())){
                apply.setDepartment(info.getDepartment());
                apply.setGrade(info.getGrade());
                apply.setMajor(info.getMajor());
                apply.setClassName(info.getClassName());
            }
        }
        return userMapper.insertUser(apply);
    }

    /**
     * 新增待审核信息
     */
    @Override
    public int insertWait (StuApply apply){
        apply.setIsDel(1);
        return userMapper.insertUser(apply);
    }

    /**
     * 若信息不一致上述代码会传空值进去，从而提示用户重新填写
     */
    @Override
    public int checkUser(StuApply apply){
        StuInfo info = userMapper.selectStuByNum(apply.getStuNum());
        if(info == null) {
            return -1;
        } else {
            if(Objects.equals(info.getName(), apply.getName())) {
                apply.setDepartment(info.getDepartment());
                apply.setGrade(info.getGrade());
                apply.setMajor(info.getMajor());
                apply.setClassName(info.getClassName());
            }
            if ((Objects.equals(apply.getDepartment(), "") || apply.getDepartment() == null) ||
                    (Objects.equals(apply.getGrade(), "") || apply.getGrade() == null) ||
                    (Objects.equals(apply.getMajor(), "") || apply.getMajor() == null) ||
                    (Objects.equals(apply.getClassName(), "") || apply.getClassName() == null)){
                return 0;
            } else {
                if(applyMapper.selectApplyByNum(apply.getStuNum()) != null) {
                    return -2;
                } else {
                    return 1;
                }
            }
        }
    }
}
